#include "PauseScene.h"
#include "D3DInput.h"

PauseScene::PauseScene(IDirect3DTexture9* background){
	mBackTex = background;
	loadTexture();

	btResume = new Button(mResume, mResumeBack, true);
	btSaveMap = new Button(mSaveMap, mSaveMapBack, false);
	btBackToStart = new Button(mBackToStart, mBackToStartBack, false);

	btResume->getPos().x = 0.0f;
	btResume->getPos().y = 0.0f;

	btSaveMap->getPos().x = 0.0f;
	btSaveMap->getPos().y = -20.0f;

	btBackToStart->getPos().x = 0.0f;
	btBackToStart->getPos().y = -40.0f;

	mBackgroundPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	mBackgroundCenter = D3DXVECTOR3((float)1920/2, (float)1080/2, 0.0f);
	count = 0.0f;
}

PauseScene::~PauseScene(){
	delete btResume;
	delete btSaveMap;
	delete btBackToStart;
	mBackTex = NULL;
	releaseTexture();
}

SceneRS PauseScene::update(float dt){
	//pos
	SceneRS rs;
	rs.ID = -1;
	rs.Info = "";
	D3DDEVICE_CREATION_PARAMETERS cparams;
	RECT rect;

	gDevice->GetCreationParameters(&cparams);
	GetWindowRect(cparams.hFocusWindow, &rect);

	mWidth = rect.right - rect.left;
	mHeight= rect.bottom - rect.top;

	if(count>0.0f){
		count-=dt;

		if(count<= 0.0f){
			count =0.0f;
		}
		return rs;
	}

	//key event
	if(gInput->keyDown(DIK_DOWNARROW)){
		if(btResume->getSelected()){
			btResume->setSelected(false);
			btSaveMap->setSelected(true);
		}else if(btSaveMap->getSelected()){
			btSaveMap->setSelected(false);
			btBackToStart->setSelected(true);
		}else{
			btBackToStart->setSelected(false);
			btResume->setSelected(true);
		}
		count = 0.2f;
		return rs;
	}

	if(gInput->keyDown(DIK_UPARROW)){
		if(btBackToStart->getSelected()){
			btBackToStart->setSelected(false);
			btSaveMap->setSelected(true);
		}else if(btSaveMap->getSelected()){
			btSaveMap->setSelected(false);
			btResume->setSelected(true);
		}else{
			btResume->setSelected(false);
			btBackToStart->setSelected(true);
		}
		count = 0.2f;
		return rs;
	}

	if(gInput->keyDown(DIK_RETURN)){
		if(btResume->getSelected()){
			rs.ID = 0; // resume
		}else if(btSaveMap->getSelected() ){
			rs.ID = 1; // save
		}else{
			rs.ID = 2; // main menu
		}
	}

	return rs;
}

void PauseScene::draw(ID3DXSprite* sprite){
	drawBackground(sprite);
	btResume->render(sprite);
	btSaveMap->render(sprite);
	btBackToStart->render(sprite);
}

void PauseScene::drawBackground(ID3DXSprite* sprite){
	if(mBackTex == 0) return;

	float rate  = 1.0f;
	float wrate = (float)mWidth/1920;
	float hrate = (float)mHeight/1080;
	if(wrate > hrate){
		rate = wrate;
	}else{
		rate = hrate;
	}
	rate = ceilf(rate * 100) / 100; 
	rate = rate/2;
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,rate,-rate,1.0f);
	sprite->SetTransform(&(S));
	sprite->Draw(mBackTex,0,&mBackgroundCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	sprite->Flush();
}

void PauseScene::loadTexture(){
	mResume = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/Resume_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mResume);

	mResumeBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/Resume_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mResumeBack);

	mSaveMap = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/Save_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mSaveMap);

	mSaveMapBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/Save_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mSaveMapBack);

	mBackToStart = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/MainMenu_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mBackToStart);

	mBackToStartBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/MainMenu_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mBackToStartBack);
}

void PauseScene::releaseTexture(){
	if(mResume != 0){
		ReleaseCOM(mResume);
	}

	if(mResumeBack != 0){
		ReleaseCOM(mResumeBack);
	}

	if(mSaveMap != 0){
		ReleaseCOM(mSaveMap);
	}

	if(mSaveMapBack != 0){
		ReleaseCOM(mSaveMapBack);
	}

	if(mBackToStart != 0){
		ReleaseCOM(mBackToStart);
	}

	if(mBackToStartBack != 0){
		ReleaseCOM(mBackToStartBack);
	}
}